﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using Sharp.DataTransferObject;
using Sharp.Domain.PlatformBoundedContext.MasterDataModule.Aggregates;

using Sharp.Infrastructure;
using Sharp.Infrastructure.Crosscutting;
using Sharp.Infrastructure.Search;

namespace Sharp.Application.Platform
{

    /// <summary>
    /// 系统支持服务
    /// </summary>
    public partial class PlatformServiceImpl
    {
        #region 主数据

        #region 保存主数据

        /// <summary>
        /// 保存主数据
        /// </summary>
        /// <param name="masterData">主数据</param>
        /// <returns>操作结果</returns>
        [Logging("保存主数据“{@p0.Name}”")]
        public Result SaveMasterData(MasterDataDTO masterData)

        {
            Result res = new Result();

            if (masterData.Id.IsEmpty())
            {
                _masterDataRepository.Add(masterData.To<MasterData>());
            }
            else
            {
                var info = _masterDataRepository.Get(masterData.Id);
                info.Name = masterData.Name;
                info.FullName = masterData.FullName;
                info.Code = masterData.Code;
                info.ParentCode = masterData.ParentCode;

                _masterDataRepository.Update(info);
            }
            Context.Commit();

            //CacheHelper.Remove("GetMasterData_" + typeof(TMasterData).Name);

            return res;
        }

        #endregion

        #region 删除主数据

        /// <summary>
        /// 删除主数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [Logging("删除主数据“{@p0}”")]
        public Result DeleteMasterData(Guid[] ids)
        {
            var res = new Result();
            if (ids == null)
            {
                res.Err = "Id参数不可为空！";
                return res;
            }

            res = _masterDataService.DeleteMasterData(ids);

            //CacheHelper.Remove("GetMasterData_" + typeof(TMasterData).Name);

            return res;
        }

        #endregion

        #region 获取一条主数据

        /// <summary>
        /// 获取一条主数据
        /// </summary>
        /// <returns></returns>
        public MasterDataDTO GetMasterDataInfo(Guid id)
        {
            return _masterDataRepository.Get(id).To<MasterDataDTO>();
        }

        #endregion

        #region 获取主数据

        /// <summary>
        /// 获取主数据
        /// </summary>
        /// <returns></returns>
        public IList<MasterDataDTO> GetMasterData()

        {
            List<MasterDataDTO> data;
            //if (CacheHelper.TryGet("GetMasterData_" + typeof(TMasterData).Name, out data))
            //{
            //    return data;
            //}

            data =
                _masterDataRepository.GetList(null, "Code ASC", (int?)null, new string[] { }).To<List<MasterDataDTO>>();
            //if (data.Any())
            //{
            //    CacheHelper.Add("GetMasterData_" + typeof(TMasterData).Name, data, 3600);
            //}
            return data;
        }

        #endregion

        #region 代码项翻译成文字项

        /// <summary>
        /// 代码项翻译成文字项
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public string Code2Text(string code)
        {
            return _masterDataService.Code2Text(code);
        }

        #endregion

        #region 获取主数据分类

        /// <summary>
        /// 获取主数据分类
        /// </summary>
        /// <returns></returns>
        public IList<MasterDataDTO> GetMasterDataSort()
        {
            return _masterDataRepository.GetMasterData().To<List<MasterDataDTO>>();
        }

        #endregion

        #region 获取分页数据

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="pf"></param>
        /// <returns></returns>
        public PagedResult<MasterDataDTO> MasterDataGetPaged(PagingFeature pf)
        {
            var paged = _masterDataRepository.Pagging(pf, null, "CODE ASC");

            return paged.To<MasterData, MasterDataDTO>();
        }

        #endregion

        #region 获取省份数据

        /// <summary>
        /// 获取省份数据
        /// </summary>
        /// <returns></returns>
        public IList<MasterDataDTO> GetProvinceData()
        {
            return _masterDataRepository
                .GetList(x => x.ParentCode == null, "Code ASC", null, new string[] { }).To<List<MasterDataDTO>>();
        }

        #endregion
        #endregion



    }
}